package com.mc.fil.service.impl;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilMultimedia;
import com.mc.fil.domain.FilSow;
import com.mc.fil.mapper.FilMultimediaMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilWateringMapper;
import com.mc.fil.domain.FilWatering;
import com.mc.fil.service.IFilWateringService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 浇水情况 浇水情况Service业务层处理
 *
 * @author mc
 * @date 2021-04-14
 */
@Service
public class FilWateringServiceImpl implements IFilWateringService
{
    @Autowired
    private FilWateringMapper filWateringMapper;
    @Autowired
    private FilMultimediaMapper multimediaMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询浇水情况 浇水情况
     *
     * @param id 浇水情况 浇水情况ID
     * @return 浇水情况 浇水情况
     */
    @Override
    public FilWatering selectFilWateringById(Long id)
    {
        FilWatering watering = filWateringMapper.selectFilWateringById(id);
        FilMultimedia multimedia = new FilMultimedia();
        multimedia.setFilId(id);
        multimedia.setGenre("filWatering");
        watering.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        return watering;
    }

    /**
     * 查询浇水情况 浇水情况列表
     *
     * @param filWatering 浇水情况 浇水情况
     * @return 浇水情况 浇水情况
     */
    @Override
    public List<FilWatering> selectFilWateringList(FilWatering filWatering)
    {
        List<FilWatering> list = filWateringMapper.selectFilWateringList(filWatering);
        for(FilWatering watering : list){
            FilMultimedia multimedia = new FilMultimedia();
            multimedia.setFilId(watering.getId());
            multimedia.setGenre("filWatering");
            watering.setPhotos(multimediaMapper.selectFilMultimediaList(multimedia));
        }
        return list;
    }

    /**
     * 新增浇水情况 浇水情况
     *
     * @param filWatering 浇水情况 浇水情况
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFilWatering(FilWatering filWatering)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filWatering.setCreateTime(DateUtils.getNowDate());
        filWatering.setCreateUserId(loginUser.getUsername());
        filWateringMapper.insertFilWatering(filWatering);
        List<FilMultimedia> list = filWatering.getPhotos();
        for(FilMultimedia fil : list){
            fil.setFilId(filWatering.getId());
            fil.setCreatedBy(loginUser.getUsername());
            fil.setCreatedTime(DateUtils.getNowDate());
            fil.setGenre("filWatering");
            multimediaMapper.insertFilMultimedia(fil);
        }
        return 1;
    }


}
